package com.lza.phone.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.lza.Exception.CustomException;
import com.lza.handle.PageHandler;
import com.lza.phone.entity.*;
import com.lza.phone.entity.connectEntity.PhoneList;
import com.lza.phone.service.CapacityService;
import com.lza.phone.service.PhoneService;
import com.lza.phone.service.UserService;
import com.lza.response.Result;
import com.lza.security.JwtAuthenticationTokenFilter;
import com.lza.security.JwtTokenUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李钊澳
 * @since 2021-08-28
 */
@RestController
@RequestMapping("/phone")
public class PhoneController {
    @Autowired
    CapacityService capacityService;
    @Autowired
    PhoneService phoneService;
    @Autowired
    UserService userService;
    @Autowired
    JwtTokenUtil jwtTokenUtil;
    @Autowired
    JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;
    //分页显示手机(不联表)
    @GetMapping("/pageAllPhone")
    public Result getPageAllPhone(@RequestBody PageHandler pageHandler){
        Page<Phone> phonePage=new Page<>(pageHandler.getSize(),pageHandler.getCurrentSize());
        IPage<Phone> page = phoneService.page(phonePage);
        return Result.ok().getData("分页显示所有手机", page);
    }
    //不分页显示所有手机(联表)
    @GetMapping("allPhone")
    public Result getAllPhone(){
        List<PhoneList> allPhone = phoneService.getAllPhone();
        return Result.ok().getData("查询所有手机成功", allPhone);
    }

    //根据手机型号来查手机(模糊查询)
    @PostMapping("/findPhone")
    public Result findPhoneByName(@RequestBody Phone phone){
        String name = phone.getPhoneName();
        QueryWrapper<Phone> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("phoneName", name);
        //相当于select * from phone where phoneName like '%name'   column相当于数据库里面的参数
        Phone one = phoneService.getOne(queryWrapper);
        if(one==null){
            return Result.error();
        }else{
            return Result.ok().getData("查出手机为:", one);
        }
    }
    //显示完整的一个手机参数
    @PostMapping("/findPhoneOne/{id}")
    public Result getOnePhone(@PathVariable("id") Integer id){
        PhoneList phone = phoneService.getOnePhone(id);
        /*try {
            int i=1/0;
        }catch(Exception e){
            //执行自定义异常
            throw new CustomException(20001,"执行自定义异常");
        }*/
        if(phone==null){
            throw new CustomException(20001, "没有该手机");
        }
        return Result.ok().getData("查到的手机", phone);
    }

    //显示要进行pk的手机
    @GetMapping("/pkPhone")
    public Result pkPhone(){
        int joinPk=1;//默认是1，不进行PK(数据库默认为0)
        QueryWrapper<Phone> queryWrapper=new QueryWrapper<>();
        QueryWrapper<Phone> show = queryWrapper.eq("joinPk", joinPk);
        List<Phone> list = phoneService.list(show);
        System.out.println("显示"+list);
        if(list.isEmpty()){//不能用==null
            return Result.error().getMessage("PK失败，你没有选择手机加入");
        }
        return Result.ok().getData("进行pk的手机",list);
    }
    //显示所有手机根据cpu排名分数排名
    @GetMapping("/allPhoneByCpu")
    public Result allPhoneByCpu(){
        List<PhoneList> allPhone = phoneService.getAllPhoneByCpuMark();
        return Result.ok().getData("查询所有手机成功", allPhone);
    }
    //添加一台手机
    @ApiOperation("添加一台手机")
    @ApiOperationSupport(author = "李钊奥")
    @PostMapping("addOnePhone")
    public Result addOnePhone(@RequestBody Phone phone ){
        phoneService.saveOnePhone(phone);
        return Result.ok().getMessage("上传手机成功");
    }
    //第二种方法

    @ApiOperation("显示所有手机，第二种方法")
    @ApiOperationSupport(author = "李钊奥")
    @PostMapping("allPhone")
    public Result allPhone( ){
        Result result = phoneService.allPhone();
        return result;
    }
}

